package com.quanquan.user.service;

import com.quanquan.user.entity.UserFormId;
import com.quanquan.user.repository.UserFormIdRepository;
import com.quanquan.user.request.UserFormIdRequest;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.UUIDGenerator;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName:UserFormService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/11/14 17:08
 * @Version 1.0
 */
@Service
public class UserFormIdService {

    @Autowired
    private UserFormIdRepository userFormIdRepository;

    /**
     * 保存formId
     *
     * @param request
     * @return
     */

    public boolean saveUserFormId(UserFormIdRequest request){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        UserFormId userFormId = new UserFormId();
        userFormId.setId(UUIDGenerator.getUUID());
        userFormId.setOpenid(request.getOpenid());
        userFormId.setFormId(request.getFormId());
        userFormId.setBtnClass(request.getBtnClass());
        userFormId.setCreateTime(sdf.format(new Date()));
        // 7天后过期
        userFormId.setExpireTime(sdf.format(DateUtils.getDateAfterDay(7)));
        userFormId.setStatus(0);
        userFormIdRepository.saveAndFlush(userFormId);
        return false;
    }

    /**
     * 查找formId
     *
     * @param openid
     * @return
     */
    public UserFormId findFormId(String openid){
        return userFormIdRepository.findOneFormId(openid);
    }


}
